<?php
/**
 * Created by 张彪.
 * User: 小马出行
 * Date: 2017/11/21
 * Time: 10:45
 */
namespace my;
ini_set("display_errors", "on");

require_once dirname(__DIR__) . '/Aliyun/api_sdk/vendor/autoload.php';

use Aliyun\Core\Config;
use Aliyun\Core\Exception\ClientException;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Dypls\Request\V20170525\BindAxbRequest;
use Aliyun\Api\Dypls\Request\V20170525\BindAxnRequest;
use Aliyun\Api\Dypls\Request\V20170525\UnbindRequest;
use think\Exception;
// 加载区域结点配置
Config::load();
class AXB
{

    static $acsClient = null;

    /**
     * 取得AcsClient
     *
     * @return DefaultAcsClient
     */
    public static function getAcsClient() {
        //产品名称:云通信流量服务API产品,开发者无需替换
        $product = "Dyplsapi";

        //产品域名,开发者无需替换
        $domain = "dyplsapi.aliyuncs.com";

        // TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
        $accessKeyId = Define::AXB_KEY; // AccessKeyId

        $accessKeySecret = Define::AXB_SECRET; // AccessKeySecret


        // 暂时不支持多Region
        $region = "cn-hangzhou";

        // 服务结点
        $endPointName = "cn-hangzhou";


        if(static::$acsClient == null) {

            //初始化acsClient,暂不支持region化
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);

            // 增加服务结点
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

            // 初始化AcsClient用于发起请求
            static::$acsClient = new DefaultAcsClient($profile);
        }
        return static::$acsClient;
    }


    /**
     * AXB绑定接口
     * @param $phoneA
     * @param $phoneB
     * @param $expire
     * @param $record
     * @param $outId
     * @return mixed
     * @throws ClientException
     */
    public static function bindAxb($phoneA,$phoneB,$expire,$record=false,$outId=0) {

        //组装请求对象-具体描述见控制台-文档部分内容
        $request = new BindAxbRequest();
        //必填:AXB关系中的A号码
        $request->setPhoneNoA($phoneA);
        //必填:AXB关系中的B号码
        $request->setPhoneNoB($phoneB);
        //必填:绑定关系对应的失效时间-不能早于当前系统时间
        $request->setExpireDate($expire);
        //可选:是否需要录制音频-默认是false
        $request->setNeedRecord($record);
        //外部业务自定义ID属性
        $request->setOutId($outId);

        //hint 此处可能会抛出异常，注意catch
        try{
            $response = static::getAcsClient()->getAcsResponse($request);
            //throw new Exception($response->Message,$response->Code);
            if($response->Code=='OK'){
                $data['status'] = 1;
                $data['data'] = (array)$response->SecretBindDTO;
            }else{
                $data['status'] = 0;
                $data['msg'] = config('axb_error_code')[$response->Code];
            }
        }catch( Exception $e ){
            $data['status'] = 0;
            $data['msg'] = $e->getMessage();
//            echo "Code={$e->getCode()}\n";
//            echo "Message={$e->getMessage()}\n";
        }
        return $data;
    }

    /**
     * AXN绑定接口
     * @param $phoneA
     * @param $phoneB
     * @param $NoType
     * @param $expire
     * @param $record
     * @param $outId
     * @return mixed
     * @throws ClientException
     */
    public static function bindAxn($phoneA,$phoneB,$NoType='NO_95',$expire,$record=false,$outId=0) {

        //组装请求对象-具体描述见控制台-文档部分内容
        $request = new BindAxnRequest();
        //必填:AXB关系中的A号码
        $request->setPhoneNoA($phoneA);
        //可选:AXN中A拨打X的时候转接到的默认的B号码,如果不需要则不设置
        $request->setPhoneNoB($phoneB);
        //必填:95中间号,NO_170代表选择使用170号码资源
        $request->setNoType($NoType);
        //必填:绑定关系对应的失效时间-不能早于当前系统时间
        $request->setExpireDate($expire);
        //可选:是否需要录制音频-默认是false
        $request->setNeedRecord($record);
        //外部业务自定义ID属性
        $request->setOutId($outId);

        //hint 此处可能会抛出异常，注意catch
        try{
            $response = static::getAcsClient()->getAcsResponse($request);
            //throw new Exception($response->Message,$response->Code);
        }catch( Exception $e ){
            echo "Code={$e->getCode()}\n";
            echo "Message={$e->getMessage()}\n";
        }
        return $response;
    }

    /**
     * 解绑接口
     * @param $subsId
     * @param $secretNo
     * @return mixed
     * @throws ClientException
     */
    public static function unbind($subsId, $secretNo) {
        //组装请求对象
        $request = new UnbindRequest();
        //必填-分配的X小哈-对应到绑定接口中返回的secretNo;
        $request->setSecretNo($secretNo);
        //可选-绑定关系对应的ID-对应到绑定接口中返回的subsId;
        $request->setSubsId($subsId);
        try{
            $response = static::getAcsClient()->getAcsResponse($request);
        }catch( Exception $e ){
            echo "Code={$e->getCode()}\n";
            echo "Message={$e->getMessage()}\n";
        }
        return $response;
    }
}